package fr.mywiki.model.ejbAdapter.services;

import javax.ejb.CreateException;
import javax.ejb.EJBException;
import javax.ejb.FinderException;
import javax.ejb.SessionBean;

import fr.mywiki.model.ejbAdapter.EntityHomeCache;
import fr.mywiki.model.ejbAdapter.ServicesAdapter;
import fr.mywiki.model.ejbAdapter.Value;
import fr.mywiki.model.ejbAdapter.entity.AccountLocalHome;
import fr.mywiki.model.ejbAdapter.entity.EntryLocalHome;
import fr.mywiki.model.ejbAdapter.entity.StructureLocalHome;
import fr.mywiki.model.ejbAdapter.entity.VersionLocalHome;
import fr.mywiki.model.ejbAdapter.services.business.CreateServiceBusiness;
import fr.mywiki.model.ejbAdapter.utils.PersistenceException;
import fr.mywiki.model.ejbAdapter.utils.Sequencer;

/**
 * This session bean deals with creating new entity beans.
 * 
 * @ejb.bean name = "CreateService"
 *		display-name = "CreateService"
 *		description = "Create Administration Service"
 *		jndi-name="ejb/MyWiki/CreateService"
 *		remote-business-interface = "fr.mywiki.model.ejbAdapter.services.business.CreateServiceBusiness"
 * 
 * @ejb.home
 * 		extends = "javax.ejb.EJBHome"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 *  
 * @ejb.interface
 * 		extends = "javax.ejb.EJBObject"
 * 		package = "fr.mywiki.model.ejbAdapter.services"
 * 
 * @author tlombard
 */
public class CreateServiceBean extends ServicesAdapter implements CreateServiceBusiness,SessionBean{
	/**
	 * The serial version UID.
	 */
	private static final long serialVersionUID = 1L;
	
	/**
	 * Default create method
	 * @throws CreateException
	 * @ejb.create-method 
	 */
	public void ejbCreate() throws CreateException {
	}
	
	/**
	 * Creates a new account object and returns the <code>Value</code> corresponding to it.
	 * 
	 * @return a <code>Value</code> object having account type.
	 * @throws PersistenceException 
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Value createAccount() throws PersistenceException {
		try {
			long newKey = Sequencer.getInstance(AccountLocalHome.JNDI_NAME).next();
			try {
				while (EntityHomeCache.getInstance().getAccountHome()
						.findByPrimaryKey(new Long(newKey)) != null)
					newKey = Sequencer.getInstance(AccountLocalHome.JNDI_NAME).next();
			} catch (FinderException e) {}
			return EntityHomeCache.getInstance().getAccountHome().create(new Long(newKey))
					.getAccountLite();
		} catch (CreateException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Creates a new entry object and returns the <code>Value</code> corresponding to it.
	 * 
	 * @return a <code>Value</code> object having entry type.
	 * @throws PersistenceException 
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Value createEntry() throws PersistenceException {
		try {
			long newKey = Sequencer.getInstance(EntryLocalHome.JNDI_NAME).next();
			try {
				while (EntityHomeCache.getInstance().getEntryHome()
						.findByPrimaryKey(new Long(newKey)) != null)
					newKey = Sequencer.getInstance(EntryLocalHome.JNDI_NAME).next();
			} catch (FinderException e) {}

			return EntityHomeCache.getInstance().getEntryHome().create(new Long(newKey))
					.getEntryLite();
		} catch (CreateException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Creates a new structure object and returns the <code>Value</code> corresponding to it.
	 * 
	 * @return a <code>Value</code> object having structure type.
	 * @throws PersistenceException 
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Value createStructure() throws PersistenceException {
		try {
			long newKey = Sequencer.getInstance(StructureLocalHome.JNDI_NAME).next();
			try {
				while (EntityHomeCache.getInstance().getStructureHome()
						.findByPrimaryKey(new Long(newKey)) != null)
					newKey = Sequencer.getInstance(StructureLocalHome.JNDI_NAME).next();
			} catch (FinderException e) {}

			return EntityHomeCache.getInstance().getStructureHome().create(new Long(newKey))
					.getStructureLite();
		} catch (CreateException e) {
			throw new EJBException(e);
		}
	}
	
	/**
	 * Creates a new version object and returns the <code>Value</code> corresponding to it.
	 * 
	 * @return a <code>Value</code> object having version type.
	 * @throws PersistenceException 
	 * 
	 * @ejb:interface-method view-type="local"
	 */
	public Value createVersion() throws PersistenceException {
		try {
			long newKey = Sequencer.getInstance(VersionLocalHome.JNDI_NAME).next();
			try {
				while (EntityHomeCache.getInstance().getVersionHome()
						.findByPrimaryKey(new Long(newKey)) != null)
					newKey = Sequencer.getInstance(VersionLocalHome.JNDI_NAME).next();
			} catch (FinderException e) {}

			return EntityHomeCache.getInstance().getVersionHome().create(new Long(newKey))
					.getVersionLite();
		} catch (CreateException e) {
			throw new EJBException(e);
		}
	}
}
